var page = 1; //当前页码
var sumpage = 0; //总页码
var page2 = 1; //当前页码
var sumpage2 = 0; //总页码
var zindex = 0; //
Page({
  data: {
    index_1: 0,
    longitude: '',
    latitude: '',
    dogdata: [],
    dogdata2: [],
    markers: [],
    polyline: [],
    circles: [],
    painting: {},
    shareImage: ''
  },
  onShareAppMessage: function () {
    return {
      title: `点击查看您附近的走失宠物，帮助它们找到家吧！`,
      imageUrl: '/img/share-local.png',
    }
  },
  onShareTimeline: function (res) {
    return {
      title: `点击查看您附近的走失宠物，帮助它们找到家吧！`,
      imageUrl: '/img/share-local.png',
    }
  },
  onLoad: function (options) {
    let slet = this;
    page = 1;
    page2 = 1;
    slet.setData({
      dogdata: [],
      dogdata2: []
    })
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        slet.setData({
          longitude: longitude,
          latitude: latitude
        })
        slet.getDogList();
        slet.getDogList2();
      }
    })
  },
  toSwitch(e) //切换
  {
    zindex = 1;
    console.log('111');
    let slet = this;
    let index_1 = e.currentTarget.dataset.index;
    let dogdata = slet.data.dogdata;
    let dogdata2 = slet.data.dogdata2;
    let markers = dogdata;
    if (index_1 == 1) {
      markers = dogdata2;
    }
    slet.setData({
      index_1: index_1,
      markers: markers,
    })
    setTimeout(function () {
      zindex = 0;
    }, 500)
  },
  lower: function () { //寻狗启事
    let slet = this;
    if (page <= sumpage) {
      page++;
      slet.getDogList(); //获取信息列表
    } else {
      wx.showToast({
        title: '到底了',
        icon: 'none',
        duration: 1000
      })
    }
  },
  lower2: function () { //寻主启事
    let slet = this;
    if (page2 <= sumpage2) {
      page2++;
      slet.getDogList(); //获取信息列表
    } else {
      wx.showToast({
        title: '到底了',
        icon: 'none',
        duration: 1000
      })
    }
  },
  getDogList() //获取信息列表
  {
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    let slet = this;
    let longitude = slet.data.longitude;
    let latitude = slet.data.latitude;
    let dogdata = slet.data.dogdata;
    wx.request({
      url: getApp().data.apiUrl + '/GetDogListPage.ashx',
      data: {
        longitude: longitude,
        latitude: latitude,
        type: 0,
        page: page,
        size: 200
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var resdata = JSON.parse(decodeURIComponent(JSON.stringify(res.data)));
        console.log(resdata);
        sumpage = resdata.sumpage;

        if (dogdata.length > 0) {
          dogdata = dogdata.concat(resdata.data);
        } else {
          dogdata = resdata.data;
        }
        let dest = dogdata;
        dest.sort(function (a, b) { //排序
          var value1 = a.distance,
            value2 = b.distance;
          return value1 - value2;
        });
        slet.setData({
          dogdata: dest,
          markers: dogdata
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  getDogList2() //获取寻主启事
  {
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    let slet = this;
    let longitude = slet.data.longitude;
    let latitude = slet.data.latitude;
    let dogdata2 = slet.data.dogdata2;
    wx.request({
      url: getApp().data.apiUrl + '/GetDogListPage.ashx',
      data: {
        longitude: longitude,
        latitude: latitude,
        type: 1,
        page: page2,
        size: 200
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var resdata = JSON.parse(decodeURIComponent(JSON.stringify(res.data)));
        console.log(resdata);
        sumpage2 = resdata.sumpage;

        if (dogdata2.length > 0) {
          dogdata2 = dogdata2.concat(resdata.data);
        } else {
          dogdata2 = resdata.data;
        }
        let dest = dogdata2;
        dest.sort(function (a, b) { //排序
          var value1 = a.distance,
            value2 = b.distance;
          return value1 - value2;
        });
        slet.setData({
          dogdata2: dest
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  markertap(e) { //点击标记时触发
    setTimeout(function () {
      console.log('222');
      if (zindex == 0) {
        wx.navigateTo({
          url: 'doginfo?id=' + e.detail.markerId,
        })
      }
      zindex = 0;
    }, 500)
  },
  toDogInfo(e) //寻狗详情
  {
    let slet = this;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'doginfo?id=' + id,
    })
  },
  toMasterInfo(e) //寻主详情
  {
    let slet = this;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'masterinfo?id=' + id,
    })
  },
})